/**
 * 中英文混合排序
 * mixedSort(array, [key])
 * array: // 排序对象
 * key: // 排序的字段
 */

const sortZh = [
    { zh: '阿', le: 'a' },
    { zh: '八', le: 'b' },
    { zh: '嚓', le: 'c' },
    { zh: '哒', le: 'd' },
    { zh: '妸', le: 'e' },
    { zh: '发', le: 'f' },
    { zh: '旮', le: 'g' },
    { zh: '哈', le: 'h' },
    { zh: '讥', le: 'j' },
    { zh: '咔', le: 'k' },
    { zh: '垃', le: 'l' },
    { zh: '麻', le: 'm' },
    { zh: '拏', le: 'n' },
    { zh: '噢', le: 'o' },
    { zh: '妑', le: 'p' },
    { zh: '七', le: 'q' },
    { zh: '呥', le: 'r' },
    { zh: '扨', le: 's' },
    { zh: '它', le: 't' },
    { zh: '穵', le: 'w' },
    { zh: '夕', le: 'x' },
    { zh: '丫', le: 'y' },
    { zh: '帀', le: 'z' }
];

function switchKey(obj, sortName, sortName2) {
    let r;
    const witch = obj[sortName] ? sortName : sortName2;
    if (obj[witch] && /^[a-zA-Z]/.test(obj[witch] ? obj[witch] : '没有')) {
        r = obj.en2zh;
    } else {
        r = obj[witch];
    }
    if (!r) {
        return obj.zh;
    }
    return r;
}

function main(receivedArr, [sortName, sortName2]) {
    let arr = [...receivedArr];
    arr = arr.concat(sortZh);
    // 暂时无法对i u v 排序
    const LETTERS = 'abcdefghjklmnopqrstwxyz'.split('');
    const ZH = '安贝苍邓妸范葛胡杰科黎迈倪噢潘全呥萨特王希杨扎'.split('');
    const iArr = [{ zh: '存在', le: 'i' }];
    const uArr = [{ zh: '存在', le: 'u' }];
    const vArr = [{ zh: '存在', le: 'v' }];
    for (let i = arr.length - 1; i >= 0; i -= 1) {
        const witch = arr[i][sortName] ? sortName : sortName2;
        if (arr[i][witch] && /^[a-zA-Z]/.test(arr[i][witch])) {
            const a = LETTERS.indexOf(arr[i][witch][0].toLowerCase());
            if (a > -1) {
                arr[i].en2zh = ZH[a];
                // eslint-disable-next-line prefer-destructuring
                arr[i].le = arr[i][witch][0];
            } else {
                const letter = arr[i][witch][0];
                let isIUV = 0;
                switch (letter) {
                    case 'I':
                    case 'i':
                        iArr.push(arr[i]);
                        isIUV = 1;
                        break;
                    case 'U':
                    case 'u':
                        uArr.push(arr[i]);
                        isIUV = 1;
                        break;
                    case 'V':
                    case 'v':
                        vArr.push(arr[i]);
                        isIUV = 1;
                        break;
                    default:
                }
                if (isIUV) {
                    arr.splice(i, 1);
                }
            }
        }
    }

    // ‘厦’比较特殊，不知道为什么，如果替换成‘吓’来排序就一切正常了。临时先这样替换吧，以后有好的排序办法在完善
    const resolveCompareStr = str => {
        if (str[0] === '厦') {
            return str.replace(/厦/, '吓');
        }
        return str;
    };

    arr.sort((param1, param2) => {
        const one = switchKey(param1, sortName, sortName2);
        const two = switchKey(param2, sortName, sortName2);

        const r = (resolveCompareStr(one) || '9').localeCompare(resolveCompareStr(two), 'zh-CN-u-co-pinyin');
        return r;
    });

    // 处理iuv
    const numI = sortZh[8];
    const positionI = arr.indexOf(numI);
    for (let i = iArr.length - 1; i >= 0; i -= 1) {
        arr.splice(positionI, 0, iArr[i]);
    }
    const UV = uArr.concat(vArr);
    const numV = sortZh[19];
    const positionV = arr.indexOf(numV);
    for (let i = UV.length - 1; i >= 0; i -= 1) {
        arr.splice(positionV, 0, UV[i]);
    }

    // 分离无法识别的项目
    const noSort = [];
    let delPosition = -1;
    for (let i = 0; i < arr.length; i += 1) {
        if (arr[i].le === 'a') {
            break;
        }
        noSort.push(arr[i]);
        delPosition = i;
    }
    arr.splice(0, delPosition + 1);
    for (let i = arr.length - 1; i >= 0; i -= 1) {
        if (/^[\u4e00-\u9fa5a-zA-Z]/.test(arr[i][sortName])) {
            break;
        }
        noSort.push(arr[i]);
        arr.splice(i, 1);
    }
    if (noSort.length > 0) {
        arr = arr.concat({ zh: '#', le: '#' }, noSort);
    }

    for (let i = arr.length - 1; i >= 0; i -= 1) {
        if (arr[i].zh) {
            arr.splice(i, 1);
        }
    }
    return arr;
}

export default main;
